/**
 * 
 */
package com.seguriapp.controllers;

import java.io.FileNotFoundException;

import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

import com.seguriapp.dao.ComplaintDao;
import com.seguriapp.dao.CrimeCategoryDao;
import com.seguriapp.entities.ComplaintEntity;
import com.seguriapp.entities.CrimeCategoryEntity;
import com.seguriapp.sync.ComplaintTask;
import com.seguriapp.sync.CrimeCategoryTask;
import com.seguriapp.sync.EntityCategoryTask;

/**
 * @author pjohnson
 */
public class ComplaintController {

	private Context mContext;

	/**
	 * @param context
	 */
	public ComplaintController(final Context context) {
		mContext = context;
	}

	/**
	 * @param complaint
	 */
	public void registerComplaint(final ComplaintEntity complaint) {
		mContext.getContentResolver().insert(ComplaintDao.CONTENT_URI,
				ComplaintDao.contentValuesToInsert(complaint));
		final ComplaintTask cTask = new ComplaintTask(mContext, complaint);
		cTask.execute();
	}

	/**
	 * Obtiene de la BD local las categorias de los crimenes
	 * 
	 * @return
	 */
	public Cursor getComplaintCategories() {
		MatrixCursor extras = new MatrixCursor(new String[] { "_id",
				"idCategory", "name", "selected", "description" });
		extras.addRow(new String[] { "-1", "-1", "Seleccione Categoria", "1",
				"Categoria" });

		Cursor cursor = mContext.getContentResolver().query(
				CrimeCategoryDao.CONTENT_URI, null, null, null, null);

		Cursor[] cursors = { extras, cursor };
		Cursor extendedCursor = new MergeCursor(cursors);
		return extendedCursor;
	}

	public Cursor getComplaintCategoriesForDialog(){
		return mContext.getContentResolver().query(
				CrimeCategoryDao.CONTENT_URI, null, null, null, null);
	}
	/**
	 * Obtiene las categorias del servidor y las inserta en la BD local.
	 */
	public void insertInitialCategories() {
		final CrimeCategoryTask task = new CrimeCategoryTask(mContext);
		task.execute();
		final EntityCategoryTask task2 = new EntityCategoryTask(mContext);
		task2.execute();

	}

	/**
	 * @param category
	 */
	public void updateSelectedCategory(final CrimeCategoryEntity category) {
		mContext.getContentResolver().update(CrimeCategoryDao.CONTENT_URI,
				CrimeCategoryDao.contentValuesToUpdate(category),
				CrimeCategoryDao.COL_CID + " = ?",
				new String[] { String.valueOf(category.getIdCategory()) });
	}

	public Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {

		// Decode image size
		BitmapFactory.Options o = new BitmapFactory.Options();
		o.inJustDecodeBounds = true;
		BitmapFactory.decodeStream(mContext.getContentResolver()
				.openInputStream(selectedImage), null, o);

		// The new size we want to scale to
		final int REQUIRED_SIZE = 100;

		// Find the correct scale value. It should be the power of 2.
		int width_tmp = o.outWidth, height_tmp = o.outHeight;
		int scale = 1;
		while (true) {
			if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
				break;
			}
			width_tmp /= 2;
			height_tmp /= 2;
			scale *= 2;
		}

		// Decode with inSampleSize
		BitmapFactory.Options o2 = new BitmapFactory.Options();
		o2.inSampleSize = scale;
		return BitmapFactory.decodeStream(mContext.getContentResolver()
				.openInputStream(selectedImage), null, o2);

	}

	public String getRealPathFromURI(Uri contentUri) {
		String[] proj = { MediaStore.Images.Media.DATA };
		Cursor cursor = (mContext.getContentResolver().query(contentUri, proj,
				null, null, null));
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}
}
